package cn.wk.libs4a.view.gallery;

import uk.co.senab.photoview.PhotoView;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ProgressBar;
import cn.wk.libs4a.WKApplication;

import com.itotem.imageloader.core.assist.FailReason;
import com.itotem.imageloader.core.assist.ImageLoadingListener;

public class WKPhotoViewForGallery extends PhotoView {
	private int defaultImageRes = 0;
	private String bigUrl;
	private ProgressBar progressBar;

	private Context mContext;

	public WKApplication app() {
		return (WKApplication) mContext.getApplicationContext();
	}

	public WKPhotoViewForGallery(Context context) {
		super(context);
		this.mContext = context;
	}

	public WKPhotoViewForGallery(Context context, AttributeSet attr, int defStyle) {
		super(context, attr, defStyle);
		this.mContext = context;
	}

	public WKPhotoViewForGallery(Context context, AttributeSet attr) {
		super(context, attr);
		this.mContext = context;
	}

	public void setProgressBar(ProgressBar bar) {
		this.progressBar = bar;
	}

	public void setBigUrl(String bigUrl) {
		this.bigUrl = bigUrl;
	}

	public String getBigUrl() {
		return bigUrl;
	}

	public void setDefault(int resId) {
		defaultImageRes = resId;
	}

	public void recycle(boolean showDefault) {
		if (showDefault) {
			if (defaultImageRes == 0) {
				setImageBitmap(null);
			} else {
				setImageResource(defaultImageRes);
			}
		}
	}

	public void reload() {
		app().imageLoader.displayImage(bigUrl, this,
				new ImageLoadingListener() {

					@Override
					public void onLoadingStarted() {
						if (progressBar != null)
							progressBar.setVisibility(View.VISIBLE);
					}

					@Override
					public void onLoadingFailed(FailReason arg0) {
						if (progressBar != null)
							progressBar.setVisibility(View.GONE);
						// WKApplication.showMsg("图片加载失败!");
					}

					@Override
					public void onLoadingComplete(Bitmap arg0) {
						if (progressBar != null)
							progressBar.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingCancelled() {
						if (progressBar != null)
							progressBar.setVisibility(View.GONE);
					}
				});
	}
}